#ifndef __PB_TOKEN_TEST_H__
#define __PB_TOKEN_TEST_H__
#include "gtest/gtest.h"
#include "stringutil.h"
#include "pb_token.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void ParseTokenResultFile(const string & filename,vector<pblite::Token> & tokens)
{
	ifstream fs(filename,ifstream::in);
	string line;
	while(getline(fs,line))
	{
		vector<string> parts;
		split(line,parts,"\t");
		pblite::Token token;
		token.text = parts[0];
		token.type = static_cast<pblite::TOKEN_TYPE>(atoi(parts[1].c_str()));
		token.index = atoi(parts[2].c_str());
		token.lineno = atoi(parts[3].c_str());
		tokens.push_back(token);
	}
}

class TestTestLexerFile:public testing::TestWithParam<const char *>
{

};


TEST_P(TestTestLexerFile,TestAll)
{
	const char *filename = GetParam();
	cout<<filename<<endl;
	vector<pblite::Token> tokens;
	ParseTokenResultFile(string(filename)+".tok",tokens);
	ASSERT_GT(tokens.size(),0);
	ifstream fs(filename,ifstream::in);
	pblite::Lexer lexer(fs);
	for(int i =0;i < tokens.size();++i)
	{
		cout<<"Testing Token"<<i<<endl;
		pblite::Token & token = tokens[i];
		pblite::Token *pToken = lexer.Next();
		pblite::Token errorToken;
		if(pToken==NULL)
		{
			pToken = lexer.SkipErrorToken(errorToken);
		}
		ASSERT_TRUE(pToken!=NULL);
		EXPECT_TRUE(pToken->text == token.text)<<"pToken->text:"<<pToken->text<<" "<<"token.text:"<<token.text;
		EXPECT_EQ(pToken->lineno,token.lineno);
		EXPECT_EQ(pToken->index,token.index);
		EXPECT_EQ(pToken->type,token.type);
	}
}

INSTANTIATE_TEST_CASE_P(pb_token,TestTestLexerFile,testing::Values("../pblite_test/data/testlexer_1"));
#endif